import { createRouter, createWebHistory } from 'vue-router'
import loginRequest from '@/api/login.js';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path:"/",
      component:()=>import("@/views/login.vue")
    }, {
      path:"/home",
      component:()=>import("@/views/home.vue"),
      children:[
        {
          path:"sys/dict",
          component:()=>import("@/views/sys/dict/dict-list.vue")
        }, {
          path:"production/process",
          component:()=>import("@/views/production/process.vue")
        }, {
          path:"production/workmanship",
          component:()=>import("@/views/production/workmanship.vue")
        },{
          path:"device/type",
          component:() =>import("@/views/device/deviceType.vue")
        },{
          path:"md/materialCat",
          component:()=>import("@/views/md/classify/classify-list.vue")
        },{
          path:"device/ledger",
          component:() =>import("@/views/device/deviceLedger.vue")
        },{
          path:"qc/defect",
          component:()=>import("@/views/qc/defect/defect.vue")
        },{
          path:"qc/testingProject",
          component:()=>import("@/views/qc/testingItem/testingItem.vue")
        },{
          path:"qc/testingTemplate",
          component:()=>import("@/views/qc/testingTemplate/testingTemplate.vue")
        },{
          path:"qc/incoming",
          component:()=>import("@/views/qc/incoming/incoming.vue")
        },{
          path:"qc/process",
          component:()=>import("@/views/qc/process/process.vue")
        },{
          path:"qc/shipment",
          component:()=>import("@/views/qc/shipment/shipment.vue")
        },{
          path:"qc/tasks",
          component:()=>import("@/views/qc/task/task.vue")
        },{
          path:"md/units",
          component:()=>import("@/views/md/unit/unit-list.vue")
        },{
          path:"device/maintainItems",
          component:() =>import("@/views/device/devicemaintainItems.vue")
        },{
          path:"device/maintainPlan",
          component:() =>import("@/views/device/devicemaintainPlan.vue")
        },{
          path:"md/material",
          component:()=>import("@/views/md/material/material-list.vue")
        },{
          path:"tools/types",
          component:()=>import("@/views/tools/toolstypes.vue")
        },{
          path:"tools/ledger",
          component:()=>import("@/views/tools/toolsledger.vue")
        },{
          path:"md/workshop",
          component:()=>import("@/views/md/workShop/workShop-list.vue")
        },{
          path:"md/workstation",
          component:()=>import("@/views/md/workstation/workstation-list.vue")
        }
      ]
    }
  ]
})

// 添加路由守卫，进行登录判断
router.beforeEach(async  (to, from) => {
  console.log("to:" + to.path);
  console.log("from:" + from.path);
  if(to.path != '/'){
    // 不是访问登录页面,访问其他页面，这时就需要判断是否登录。
    // 判断策略就是将token发送到服务器端进行判断。  但是token在哪？
    // 获取token
    let token = localStorage.getItem("token")
    // 发送请求进行校验
    let result = await  loginRequest.checkLogin(token);
    console.log(result)
    if(result.code == '200'){
      return true;
    }else if(result.code == '507'){// 续签成功
      // 续签之后，我们要将最新的token存储在localStorage中，替换原来的token
      localStorage.setItem("token",result.msg);
      return true;
    }else{
      return "/";
    }
  }else {
    // 返回 false 以取消导航
    return true;
  }
})
export default router